﻿<Window
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:app="clr-namespace:Yorrick.FlickrMetadataSynchr"
	xmlns:sync="clr-namespace:Yorrick.FlickrMetadataSynchr.Sync"
	xmlns:flickr="clr-namespace:Yorrick.FlickrMetadataSynchr.Flickr"
	xmlns:common="clr-namespace:Yorrick.Common"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
	x:Class="Yorrick.FlickrMetadataSynchr.MainWindow"
	Title="Flickr Metadata Synchr"
	Height="Auto" Width="570"
	MinHeight="560" MinWidth="570"
	Icon="Resources\app.ico"
	common:WindowSettings.Save="True"
	Loaded="MainWindow_Loaded"
	Margin="0,0,0,0"
	x:Name="TheMainWindow" Initialized="MainWindow_Initialized"
	Foreground="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
	Closing="MainWindow_Closing"
>
	<Window.Resources>
		<ResourceDictionary>

			<flickr:FlickrHelper x:Key="{x:Type flickr:FlickrHelper}" />

			<sync:SynchronizationWorker x:Key="{x:Type sync:SynchronizationWorker}" />

			<flickr:ProVisibilityConverter x:Key="{x:Type flickr:ProVisibilityConverter}" />

            <flickr:PhotosetNameConverter x:Key="{x:Type flickr:PhotosetNameConverter}" />
            
            <common:BooleanNegator x:Key="{x:Type common:BooleanNegator}" />

			<common:EnumMatchToBooleanConverter x:Key="{x:Type common:EnumMatchToBooleanConverter}" />

			<DataTemplate x:Key="PhotosetTemplate">
				<Grid>
					<Grid.ColumnDefinitions>
						<ColumnDefinition MaxWidth="32" />
						<ColumnDefinition />
					</Grid.ColumnDefinitions>
					<Border Grid.Column="0" BorderBrush="Gray" BorderThickness="1" Width="Auto" Height="Auto">
						<Image Source="{Binding Path=PhotosetSquareThumbnailUrl}" MaxWidth="32" />
					</Border>
					<Grid Grid.Column="1" Margin="5,0,0,0">
                        <TextBlock VerticalAlignment="Center" Text="{Binding Path=.,Converter={StaticResource {x:Type flickr:PhotosetNameConverter}}}" />
                    </Grid>
				</Grid>
			</DataTemplate>

		</ResourceDictionary>
	</Window.Resources>
	<Window.CommandBindings>
		<CommandBinding Command="app:MainWindow.ConnectToFlickr"
			Executed="MainWindow_ConnectToFlickr"
			CanExecute="MainWindow_CanConnectToFlickr" />
		<CommandBinding Command="app:MainWindow.AuthorizeApplication"
			Executed="MainWindow_AuthorizeApplication"
			CanExecute="MainWindow_CanAuthorizeApplication" />
		<CommandBinding Command="app:MainWindow.ForgetCredentials"
			Executed="MainWindow_ForgetCredentials"
			CanExecute="MainWindow_CanForgetCredentials" />
		<CommandBinding Command="app:MainWindow.BrowseForLocalFolder"
			Executed="MainWindow_BrowseForLocalFolder"
			CanExecute="MainWindow_CanBrowseForLocalFolder" />
		<CommandBinding Command="app:MainWindow.SyncMetadata"
			Executed="MainWindow_SyncMetadata"
			CanExecute="MainWindow_CanSyncMetadata" />
		<CommandBinding Command="app:MainWindow.CancelSyncMetadata"
		    Executed="MainWindow_CancelSyncMetadata"
			CanExecute="MainWindow_CanCancelSyncMetadata" />
        <CommandBinding Command="app:MainWindow.ToggleLogWindow" 
                        Executed="MainWindow_ToggleLogWindow"
                        />
	</Window.CommandBindings>
	<Window.InputBindings>
		<KeyBinding Command="app:MainWindow.ConnectToFlickr" Key="F4" />
		<KeyBinding Command="app:MainWindow.AuthorizeApplication" Key="F3" />
		<KeyBinding Command="app:MainWindow.BrowseForLocalFolder" Key="F10" />
		<KeyBinding Command="app:MainWindow.SyncMetadata" Key="F5" />
		<KeyBinding Command="app:MainWindow.CancelSyncMetadata" Key="Esc" />
        <KeyBinding Command="app:MainWindow.ToggleLogWindow" Gesture="CTRL+L" />
    </Window.InputBindings>
	<Window.Background>
		<LinearGradientBrush SpreadMethod="Pad" EndPoint="1,0.5" StartPoint="0,0.5">
			<GradientStop Color="#FFE4EAFF" Offset="0"/>
			<GradientStop Color="#FF8EA4F0" Offset="1"/>
		</LinearGradientBrush>
	</Window.Background>
	<Grid Margin="8,8,8,8" x:Name="MainContainerPanel">
		<Grid.RowDefinitions>
			<RowDefinition Height="Auto"/>
			<RowDefinition Height="Auto"/>
			<RowDefinition Height="Auto"/>
			<RowDefinition Height="Auto"/>
			<RowDefinition Height="Auto"/>
			<RowDefinition Height="Auto"/>
			<RowDefinition Height="Auto"/>
			<RowDefinition Height="Auto"/>
			<RowDefinition Height="*"/>
		</Grid.RowDefinitions>

		<StackPanel x:Name="AppDescriptionPanel" Height="Auto" VerticalAlignment="Top">

			<TextBlock TextWrapping="Wrap"
				FontSize="14" Foreground="#FF000000" Margin="4,0,0,8" Width="Auto" Height="Auto">This application allows you to sync metadata on Flickr with locally stored images</TextBlock>

		</StackPanel>


		<WrapPanel x:Name="ConnectToFlickrPanel" Height="Auto" ItemHeight="34" VerticalAlignment="Top" Grid.Row="1">

			<Button RenderTransformOrigin="3.147,2.368"
				Command="app:MainWindow.ConnectToFlickr"
				Content="{Binding Path=Command.Text, RelativeSource={RelativeSource Self}}"
				x:Name="Connect"
				Height="Auto" Width="144" ToolTip="Click to connect the application to Flickr."
			/>

			<Button RenderTransformOrigin="3.147,2.368"
				Command="app:MainWindow.AuthorizeApplication"
				Content="{Binding Path=Command.Text, RelativeSource={RelativeSource Self}}"
				x:Name="Authorize"
				Width="144"
				Height="Auto"
				Margin="4,0,0,0" ToolTip="Click to authorize the application to access your Flickr account. This pops up a dialog hosting the Flickr authentication and authorization process."
			/>

			<Button RenderTransformOrigin="3.147,2.368"
				Command="app:MainWindow.ForgetCredentials"
				Content="{Binding Path=Command.Text, RelativeSource={RelativeSource Self}}"
				x:Name="ForgetMe"
				Width="144"
				Height="Auto"
				Margin="4,0,0,0" ToolTip="Click to forget the Flick user account credentials for this application. Useful when using a shared device, or when you want to switch to using another Flickr account."
			/>

		</WrapPanel>
		<Grid x:Name="ConnectionInfoGrid" Height="Auto" VerticalAlignment="Top" Grid.Row="2" Margin="0,8,0,0">
			<Grid.ColumnDefinitions>
				<ColumnDefinition Width="Auto" />
				<ColumnDefinition Width="*" />
				<ColumnDefinition Width="Auto" />
				<ColumnDefinition Width="Auto" />
			</Grid.ColumnDefinitions>

			<Label
				IsEnabled="{Binding Path=IsConnected, Mode=OneWay, Source={StaticResource {x:Type flickr:FlickrHelper}}}"
				Content="Connected for user:" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0"/>

			<Label Foreground="Black"
				x:Name="UserName" Width="Auto"
				Content="{Binding Path=UserName, Mode=OneWay, Source={StaticResource {x:Type flickr:FlickrHelper}}, FallbackValue=Hidden}"
				Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1,1,1,1" HorizontalAlignment="Stretch" Margin="4,0,0,0" VerticalAlignment="Center" Height="30" Grid.Column="1" ToolTip="Shows the name of the logged in Flickr user when connected to Flickr."/>

			<Border BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Width="48" Background="#BB7B92E5" HorizontalAlignment="Left" Margin="4,0,0,0" Grid.Column="2">
				<Image x:Name="UserIcon" HorizontalAlignment="Left" Width="48" Height="48"
					Source="{Binding Path=CurrentUser.BuddyIconUrl, Mode=OneWay, Source={StaticResource {x:Type flickr:FlickrHelper}}}" ToolTip="The account picture of the logged in Flickr user, when logged in to Flickr."   />
			</Border>

			<Image x:Name="ProImage" Width="20"
				Visibility="{Binding Path=IsFlickrProUser, Converter={StaticResource {x:Type flickr:ProVisibilityConverter}}, Mode=OneWay, Source={StaticResource {x:Type flickr:FlickrHelper}}}"
				Source="pack://application:,,,/Images/FlickrPro.png"  Opacity="1" Stretch="None"   HorizontalAlignment="Right" Margin="4,0,0,0" Grid.Column="3"   />

		</Grid>
		<StackPanel x:Name="SelectPhotosetPanel" Height="Auto" VerticalAlignment="Top" Grid.Row="3">

			<Label Content="Select a Flickr photoset:" Height="Auto" Width="Auto" IsEnabled="True" HorizontalAlignment="Left"/>

			<ComboBox x:Name="ListOfPhotosets"
				IsEnabled="{Binding Path=IsConnected, Mode=OneWay, Source={StaticResource {x:Type flickr:FlickrHelper}}}"
				IsSynchronizedWithCurrentItem="True" ItemTemplate="{StaticResource PhotosetTemplate}"
				ItemsSource="{Binding Path=Photosets, Mode=OneWay, NotifyOnTargetUpdated=True, Source={StaticResource {x:Type flickr:FlickrHelper}}}"
				Height="38" SelectionChanged="ListOfPhotosets_SelectionChanged" TargetUpdated="ListOfPhotosets_TargetUpdated" Width="Auto" ToolTip="Select a photoset on Flickr from the list." />

		</StackPanel>
		<StackPanel x:Name="SelectLocalFolderPanel" Grid.Row="4">

			<Label Width="Auto" Height="Auto" Content="Select a local folder:" HorizontalAlignment="Left"/>

			<Grid Width="Auto" Height="Auto" Margin="0,0,0,4" x:Name="LocalFolderGrid">
				<Grid.ColumnDefinitions>
					<ColumnDefinition Width="Auto" />
					<ColumnDefinition Width="*" />
				</Grid.ColumnDefinitions>

				<Button RenderTransformOrigin="3.147,2.368"
					Command="app:MainWindow.BrowseForLocalFolder"
					Content="{Binding Path=Command.Text, RelativeSource={RelativeSource Self}}"
					x:Name="Browse"
					Width="144"
					HorizontalAlignment="Left" Height="34" ToolTip="Click to select (another) local folder."
				/>

				<Label  x:Name="LocalFolder"
					Content="{Binding Path=LocalFolder, Mode=OneWay, Source={StaticResource {x:Type sync:SynchronizationWorker}}}"
					Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1,1,1,1" Width="Auto" HorizontalAlignment="Stretch" Height="30" Margin="4,0,0,0" Grid.Column="1"/>

			</Grid>

			<StackPanel Orientation="Vertical" x:Name="SyncPanel" Height="Auto" VerticalAlignment="Bottom" Grid.Row="6">

			</StackPanel>



		</StackPanel>
		<StackPanel Margin="0,0,4,0" x:Name="SettingsPanel" Grid.Row="5" Orientation="Horizontal">

			<GroupBox Header="Settings" Margin="0,0,0,0" Width="Auto" DataContext="{StaticResource {x:Type sync:SynchronizationWorker}}">
				<StackPanel HorizontalAlignment="Left" x:Name="CheckBoxPanel" Width="Auto" Height="Auto" Margin="0,0,0,0" VerticalAlignment="Bottom">

					<CheckBox
						x:Name="IncludeSubFolders"
						IsChecked="{Binding Path=IncludeSubFolders, Mode=TwoWay}"
						IsEnabled="{Binding Path=BusySynching, Converter={StaticResource {x:Type common:BooleanNegator}}, Mode=OneWay}"
						Content="Include sub folders" Width="Auto" Height="Auto"
				        VerticalAlignment="Stretch" ToolTip="When enabled also looks at pictures in the subfolders of the selected local folder."
				/>

					<CheckBox
						x:Name="DownloadMissingPictures"
						IsChecked="{Binding Path=DownloadMissingPictures, Mode=TwoWay}"
						IsEnabled="{Binding Path=CanDownloadPictures, Mode=OneWay}"
						Content="Download missing pictures from Flickr " Margin="0,4,0,0" Width="Auto" Height="Auto" VerticalAlignment="Stretch" ToolTip="If enabled downloads pictures from Flickr and saves them in the local folder if no matching picture is already present."/>

                    <CheckBox
                        x:Name="UseAlternateLocalMetadataMappingControl"
						IsChecked="{Binding Path=UseAlternateLocalMetadataMapping, Mode=TwoWay}"
						IsEnabled="{Binding Path=BusySynching, Converter={StaticResource {x:Type common:BooleanNegator}}, Mode=OneWay}"
						Content="Use alternate metadata mapping" Margin="0,4,0,0" Width="Auto" Height="Auto" VerticalAlignment="Stretch" ToolTip="If enabled, uses an alternate metadata mapping for title and description that is more compatible with Adobe tools like Bridge."/>
                    
                    <CheckBox x:Name="SimulateUpdatingMetadata" Content="Simulate updating metadata"
						IsChecked="{Binding Path=SimulateUpdatingMetadata, Mode=TwoWay}"
						IsEnabled="{Binding Path=BusySynching, Converter={StaticResource {x:Type common:BooleanNegator}}, Mode=OneWay}"
						Width="Auto" Height="Auto"
						Margin="0,4,0,0"
				        VerticalAlignment="Stretch" ToolTip="If enabled, does not actually change the metadata on Flickr or of local pictures. Inspect the activity log window to see if the (simulated) activity is taking the expected actions before doing a real sync."
				/>

                    <CheckBox
                        DataContext="{Binding ElementName=TheMainWindow}"
						x:Name="ShowLogWindowControl"
						IsChecked="{Binding Path=ShowLogWindow, Mode=TwoWay}"
						Content="Show activity log window" Margin="0,4,0,0" Width="Auto" Height="Auto" VerticalAlignment="Stretch" ToolTip="If enabled, shows a secondary window that displays the activity log for the sync process."/>
                </StackPanel>
			</GroupBox>

			<GroupBox Header="Match strategy" Margin="8,0,0,0" Width="Auto" DataContext="{StaticResource {x:Type sync:SynchronizationWorker}}" ToolTip="Select a strategy on how to match pictures on Flickr with locally stored pictures.">
				<StackPanel Width="Auto" HorizontalAlignment="Left" Margin="4,0,0,0" x:Name="MatchBehaviorRadioButtonPanel" VerticalAlignment="Top" Height="Auto">
					<RadioButton Content="Smart match" GroupName="MatchBehavior"
						IsEnabled="{Binding Path=BusySynching, Converter={StaticResource {x:Type common:BooleanNegator}}, Mode=OneWay}"
						IsChecked="{Binding Path=MatchBehavior, Mode=TwoWay, Converter={StaticResource {x:Type common:EnumMatchToBooleanConverter}}, ConverterParameter=SmartMatch}" ToolTip="First match on Flickr id, then on date taken and then on title/filename." />
					<RadioButton Content="Only on date taken" GroupName="MatchBehavior}"
						IsEnabled="{Binding Path=BusySynching, Converter={StaticResource {x:Type common:BooleanNegator}}, Mode=OneWay}"
						IsChecked="{Binding Path=MatchBehavior, Mode=TwoWay, Converter={StaticResource {x:Type common:EnumMatchToBooleanConverter}}, ConverterParameter=DateTaken}" Margin="0,4,0,0" ToolTip="Only match on date taken for the Flickr picture and the locally stored picture."/>
					<RadioButton Content="Only on title/filename" GroupName="MatchBehavior"
						IsEnabled="{Binding Path=BusySynching, Converter={StaticResource {x:Type common:BooleanNegator}}, Mode=OneWay}"
						IsChecked="{Binding Path=MatchBehavior, Mode=TwoWay, Converter={StaticResource {x:Type common:EnumMatchToBooleanConverter}}, ConverterParameter=FileName}" Margin="0,4,0,0" ToolTip="Only match on title on Flickr with the filename of the locally stored picture (excluding the extension)." />
				</StackPanel>
			</GroupBox>

			<GroupBox Header="Tags sync behavior" Margin="8,0,0,0" Width="Auto" Height="Auto" DataContext="{StaticResource {x:Type sync:SynchronizationWorker}}" ToolTip="Select a behavior for how tags should be handled.">
				<StackPanel Width="Auto" HorizontalAlignment="Left" Margin="4,0,0,0" x:Name="TagsSyncBehaviorRadioButtonPanel" VerticalAlignment="Top" Height="Auto">
					<RadioButton Content="Merge" GroupName="TagsSyncBehavior}"
						IsEnabled="{Binding Path=BusySynching, Converter={StaticResource {x:Type common:BooleanNegator}}, Mode=OneWay}"
						IsChecked="{Binding Path=TagsSyncBehavior, Mode=TwoWay, Converter={StaticResource {x:Type common:EnumMatchToBooleanConverter}}, ConverterParameter=Merge}" ToolTip="Tags on Flickr are merged with those in the locally stored picture and then all tags are stored on Flickr and in the locally stored picture."/>
					<RadioButton Content="Newest wins" GroupName="TagsSyncBehavior"
						IsEnabled="{Binding Path=BusySynching, Converter={StaticResource {x:Type common:BooleanNegator}}, Mode=OneWay}"
						IsChecked="{Binding Path=TagsSyncBehavior, Mode=TwoWay, Converter={StaticResource {x:Type common:EnumMatchToBooleanConverter}}, ConverterParameter=NewestWins}" Margin="0,4,0,0" ToolTip="The tags from the last modified version win. All other tags are removed from the older version."/>
					<RadioButton Content="No sync" GroupName="TagsSyncBehavior"
						IsEnabled="{Binding Path=BusySynching, Converter={StaticResource {x:Type common:BooleanNegator}}, Mode=OneWay}"
						IsChecked="{Binding Path=TagsSyncBehavior, Mode=TwoWay, Converter={StaticResource {x:Type common:EnumMatchToBooleanConverter}}, ConverterParameter=None}" Margin="0,4,0,0" ToolTip="Do not sync tags are all. They are left unchanged on Flickr and in the locally stored picture." />
				</StackPanel>
			</GroupBox>

		</StackPanel>

		<StackPanel Width="Auto" Orientation="Horizontal" x:Name="SyncButtonPanel" d:LayoutOverrides="Width" Margin="0,8,0,0" Height="34" Grid.Row="6">

			<Button
				Command="app:MainWindow.SyncMetadata"
				Content="{Binding Path=Command.Text, RelativeSource={RelativeSource Self}}"
				x:Name="Sync" Width="144"
				RenderTransformOrigin="0.485,1.031" Height="Auto"
				Margin="0,0,0,0" ToolTip="Click to start the sync process."
				/>

			<Button
				x:Name="Cancel"
				Command="app:MainWindow.CancelSyncMetadata"
				Content="{Binding Path=Command.Text, RelativeSource={RelativeSource Self}}"
				RenderTransformOrigin="0.485,1.031" Width="144" Height="Auto"
				Margin="4,0,0,0" ToolTip="Click to cancel the sync process."
				/>

		</StackPanel>

		<StackPanel x:Name="StatusPanel" Height="Auto" VerticalAlignment="Bottom" Grid.Row="7">
			<Label d:LayoutOverrides="Width"
				Content="Status:" RenderTransformOrigin="1.517,0.395" Height="Auto" HorizontalAlignment="Left" />
            <ProgressBar
				x:Name="SyncProgressBar"
				Value="{Binding SyncProgressPercentage, ElementName=TheMainWindow, Mode=Default}"
				Height="30"
				Width="Auto" ToolTip="Progress bar showing the progress of a sync process when one is running."
			/>

        </StackPanel>

		<Border Background="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1,1,1,1" Height="Auto" x:Name="StatusMessageContainer" VerticalAlignment="Stretch" Margin="0,4,0,0" Grid.Row="8">
			<TextBlock x:Name="StatusMessageDisplay" VerticalAlignment="Stretch" Text="{Binding Path=StatusMessage, ElementName=TheMainWindow, Mode=OneWay}" TextWrapping="Wrap" Height="Auto" Background="{x:Null}" Foreground="#FF000000" ToolTip="Displays the latest status message about the running sync process or the last run sync process."/>
		</Border>

	</Grid>
</Window>
